package com.kgc.kmall.user.service;

import com.alibaba.fastjson.JSON;
import com.kgc.kmall.bean.Member;
import com.kgc.kmall.bean.MemberExample;
import com.kgc.kmall.bean.MemberReceiveAddress;
import com.kgc.kmall.bean.MemberReceiveAddressExample;
import com.kgc.kmall.service.MemberService;
import com.kgc.kmall.user.mapper.MemberMapper;
import com.kgc.kmall.user.mapper.MemberReceiveAddressMapper;
import com.kgc.kmall.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import redis.clients.jedis.Jedis;


import javax.annotation.Resource;
import java.util.List;
@Service
public class MemberServiceImpl implements MemberService{
    @Resource
    MemberMapper memberMapper;
    @Resource
    MemberReceiveAddressMapper memberReceiveAddressMapper;
    @Resource
    RedisUtil redisUtil;
    @Override
    public List<Member> selectAllMember() {
        return memberMapper.selectByExample(null);
    }

    @Override
    public Member login(String username, String password) {
        Jedis jedis =null;
        try {
         jedis = redisUtil.getJedis();
        if(jedis!=null){
            String umsMemberStr  = jedis.get("user:" + username + ":info");
        if(StringUtils.isNotBlank(umsMemberStr)){
            //redis中有
            Member member = JSON.parseObject(umsMemberStr, Member.class);
            if(member.getPassword().equals(password)){
                return member;
            }else{
                return null;
            }
        }else{
            //没有,从数据库查询
            MemberExample example=new MemberExample();
            example.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
            List<Member> members = memberMapper.selectByExample(example);
            if(members!=null&&members.size()>0){
                jedis.setex("user:" + username + ":info",60*60*24, JSON.toJSONString(members.get(0)));
                return members.get(0);
            }
        } }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
        return null;
    }

    @Override
    public void addUserToken(String token, Long memberId) {
        Jedis jedis = redisUtil.getJedis();
        jedis.setex("user:"+memberId+":token",60*60*2,token);
        jedis.close();
    }

    @Override
    public Member checkOauthUser(Long sourceUid) {
        MemberExample example=new MemberExample();
        example.createCriteria().andSourceUidEqualTo(sourceUid);
        List<Member> members = memberMapper.selectByExample(example);
        if(members!=null&&members.size()>0){
            return members.get(0);
        }
        return null;
    }

    @Override
    public void addOauthUser(Member umsMember) {
        memberMapper.insertSelective(umsMember);
    }

    @Override
    public List<MemberReceiveAddress> getReceiveAddressByMemberId(Long aLong) {
        MemberReceiveAddressExample example=new MemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(aLong);
        return memberReceiveAddressMapper.selectByExample(example);
    }

    @Override
    public MemberReceiveAddress getReceiveAddressById(long l) {
        return memberReceiveAddressMapper.selectByPrimaryKey(l);
    }
}
